<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0,1)">添加权限</el-button>

      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPermission(row.id,2)">添加</el-button>

            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>

            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>

          </template>

        </el-table-column>

      </el-table>

    </div>
    <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="onCancel">
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px" class="form-box">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width: 90%;" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width: 90%;" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width: 90%;" />
        </el-form-item>
        <el-form-item label="开启" class="switch-box">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="mini" @click="onSubmit">确定</el-button>
          <el-button size="mini" @click="onCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>

</template>

<script>
import { getPermissionDetailAPI } from '@/api/permission'
import { updatePermissionAPI } from '@/api/permission'
import { addPermissionAPI } from '@/api/permission'
import { delPermissionAPI } from '@/api/permission'
import { getPermissionListAPI } from '@/api/permission'
import { arrayToTree } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: 0, // 1为页面路由权限点, 2为按钮权限点
        pid: 0,
        enVisible: ''// 权限点开启状态
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '添加'
    }
  },
  watch: {
    showDialog(val) {
      if (!val) {
        this.$refs.perForm.resetFields()
        this.formData = {
          name: '',
          code: '',
          description: '',
          type: 0,
          pid: 0,
          enVisible: ''
        }
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionListAPI()
      this.list = arrayToTree(res)
    },
    onCancel() {
      this.showDialog = false
    },
    async onSubmit() {
      await this.$refs.perForm.validate()
      if (this.formData.id) {
        await updatePermissionAPI(this.formData)
      } else {
        await addPermissionAPI(this.formData)
      }
      this.$message.success(this.formData.id ? '编辑成功' : '添加成功')
      this.getPermissionList()
      this.showDialog = false
    },
    addPermission(pid, type) {
      // pid:  页面权限点pid值为'0', 按钮权限点值为所属页面权限点的id值
      // type: 1为页面路由权限点, 2为按钮权限点
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    async editPermission(id) {
      this.formData = await getPermissionDetailAPI(id)
      this.showDialog = true
    },
    async delPermission(id) {
      // 调用删除接口
      await delPermissionAPI(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    }
  }
}
</script>

<style>
.btn-add {
  margin: 10px;
}
.form-box {
  padding: 0px 20px;
}
.switch-box {
  margin-left: 20px;
}
</style>
